package com.gurid.service.edu.controller.admin;

import com.gurid.common.base.result.R;
import com.gurid.common.base.result.ResultCode;
import com.gurid.common.base.util.ExceptionUtils;
import com.gurid.service.base.exception.GuridException;
import com.gurid.service.edu.entity.vo.SubjectVo;
import com.gurid.service.edu.service.SubjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author Gurid
 * @since 2024-04-29
 */
//@CrossOrigin //允许跨域
@Tag(name = "SubjectController", description = "课程分类管理")
@RestController
@RequestMapping("/admin/edu/subject")
@Slf4j
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @Operation(summary = "Excel批量导入课程分类")
    @PostMapping("import")
    public R batchImport(@Parameter(description = "Excel文件", required = true) @RequestParam("file") MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            subjectService.batchImport(inputStream);
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new GuridException(ResultCode.EXCEL_DATA_IMPORT_ERROR);//抛出自定义异常
        }
        return R.ok().message("批量导入成功");
    }

    @Operation(summary = "嵌套数据列表")
    @GetMapping("nested-list")
    public R nestedList() {
        List<SubjectVo> subjectVoList = subjectService.nestedList();
        return R.ok().data("items", subjectVoList);
    }
}
